package crosshairs;

import common.ChartUtil;
import common.DemoPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.ui.ApplicationFrame;
import org.jfree.data.Range;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.text.SimpleDateFormat;

public class CrosshairDemo3 extends ApplicationFrame {

    static class MyDemoPanel extends DemoPanel implements ChangeListener {
        private JFreeChart freeChart;
        private JSlider slider;

        private JFreeChart createchart(XYDataset xyDataset) {
            JFreeChart freeChart = ChartFactory.createTimeSeriesChart("Legal & General Unit Trust Prices",
                    "Date", "Price Per Unit",
                    xyDataset, true, true, false);
            XYPlot xyPlot = (XYPlot) freeChart.getPlot();
            xyPlot.setDomainCrosshairVisible(true);
            xyPlot.setDomainCrosshairLockedOnData(false);
            xyPlot.setRangeCrosshairVisible(false);

            XYItemRenderer xyItemRenderer = xyPlot.getRenderer();
            if (xyItemRenderer instanceof XYLineAndShapeRenderer) {
                XYLineAndShapeRenderer xyLineAndShapeRenderer = (XYLineAndShapeRenderer) xyItemRenderer;
                xyLineAndShapeRenderer.setDefaultShapesVisible(true);
                xyLineAndShapeRenderer.setDefaultShapesFilled(true);
            }
            DateAxis dateAxis = (DateAxis) xyPlot.getDomainAxis();
            dateAxis.setDateFormatOverride(new SimpleDateFormat("MM-yyyy"));
            return freeChart;
        }

        private XYDataset createDataset() {
            TimeSeries timeseries = new TimeSeries("L&G European Index Trust");
            timeseries.add(new Month(2, 2001), 181.80000000000001D);
            timeseries.add(new Month(3, 2001), 167.30000000000001D);
            timeseries.add(new Month(4, 2001), 153.80000000000001D);
            timeseries.add(new Month(5, 2001), 167.59999999999999D);
            timeseries.add(new Month(6, 2001), 158.80000000000001D);
            timeseries.add(new Month(7, 2001), 148.30000000000001D);
            timeseries.add(new Month(8, 2001), 153.90000000000001D);
            timeseries.add(new Month(9, 2001), 142.69999999999999D);
            timeseries.add(new Month(10, 2001), 123.2D);
            timeseries.add(new Month(11, 2001), 131.80000000000001D);
            timeseries.add(new Month(12, 2001), 139.59999999999999D);
            timeseries.add(new Month(1, 2002), 142.90000000000001D);
            timeseries.add(new Month(2, 2002), 138.69999999999999D);
            timeseries.add(new Month(3, 2002), 137.30000000000001D);
            timeseries.add(new Month(4, 2002), 143.90000000000001D);
            timeseries.add(new Month(5, 2002), 139.80000000000001D);
            timeseries.add(new Month(6, 2002), 137D);
            timeseries.add(new Month(7, 2002), 132.80000000000001D);

            TimeSeries timeseries1 = new TimeSeries("L&G UK Index Trust");
            timeseries1.add(new Month(2, 2001), 129.59999999999999D);
            timeseries1.add(new Month(3, 2001), 123.2D);
            timeseries1.add(new Month(4, 2001), 117.2D);
            timeseries1.add(new Month(5, 2001), 124.09999999999999D);
            timeseries1.add(new Month(6, 2001), 122.59999999999999D);
            timeseries1.add(new Month(7, 2001), 119.2D);
            timeseries1.add(new Month(8, 2001), 116.5D);
            timeseries1.add(new Month(9, 2001), 112.7D);
            timeseries1.add(new Month(10, 2001), 101.5D);
            timeseries1.add(new Month(11, 2001), 106.09999999999999D);
            timeseries1.add(new Month(12, 2001), 110.3D);
            timeseries1.add(new Month(1, 2002), 111.7D);
            timeseries1.add(new Month(2, 2002), 111D);
            timeseries1.add(new Month(3, 2002), 109.59999999999999D);
            timeseries1.add(new Month(4, 2002), 113.2D);
            timeseries1.add(new Month(5, 2002), 111.59999999999999D);
            timeseries1.add(new Month(6, 2002), 108.8D);
            timeseries1.add(new Month(7, 2002), 101.59999999999999D);

            TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
            timeseriescollection.addSeries(timeseries);
            timeseriescollection.addSeries(timeseries1);
            return timeseriescollection;
        }

        @Override
        public void stateChanged(ChangeEvent e) {
            int i = slider.getValue();
            XYPlot xyPlot = (XYPlot) freeChart.getPlot();
            ValueAxis valueAxis = xyPlot.getDomainAxis();
            Range range = valueAxis.getRange();
            double d = valueAxis.getLowerBound() + ((double) i / 100D) * range.getLength();
            xyPlot.setDomainCrosshairValue(d);
        }

        public MyDemoPanel() {
            super(new BorderLayout());
            XYDataset xyDataset = createDataset();
            freeChart = createchart(xyDataset);
            addChart(freeChart);

            ChartPanel chartPanel = new ChartPanel(freeChart);
            chartPanel.setPreferredSize(new Dimension(500, 270));
            chartPanel.setDomainZoomable(true);

            JPanel jPanel = new JPanel(new BorderLayout());
            slider = new JSlider(0, 100, 50);
            slider.addChangeListener(this);
            jPanel.add(slider);

            add(chartPanel);
            add(jPanel, BorderLayout.SOUTH);
        }
    }

    public CrosshairDemo3(String title) {
        super(title);
        setContentPane(createDemoPanel());
    }

    public static JPanel createDemoPanel() {
        return new MyDemoPanel();
    }

    public static void main(String[] args) {
        CrosshairDemo3 demo3 = new CrosshairDemo3("CrosshairDemo3");
        ChartUtil.showChartDemo(demo3);
    }
}
